package com.ricchieri.manage.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.CharterExpenseFilter;
import com.ricchieri.lazymodel.CharterExpenseLazyDataModel;
import com.ricchieri.model.CharterExpense;
import com.ricchieri.model.CharterExpenseType;
import com.ricchieri.service.interf.ICharterExpenseService;
import com.ricchieri.service.interf.ICharterExpenseTypeService;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "searchCharterExpenseMB")
@ViewScoped
public class SearchCharterExpenseManageBean extends GenericManageBean {

	private static final long serialVersionUID = 4815035583616445619L;

	@ManagedProperty(value = "#{CharterExpenseTypeService}")
	ICharterExpenseTypeService charterExpenseTypeService;
	
	@ManagedProperty(value = "#{CharterExpenseService}")
	ICharterExpenseService charterExpenseService;
	
	private Map<String,String> charterExpensivesTypes = null;
	
	private CharterExpenseLazyDataModel charterExpenseLazyDataModel;
	private CharterExpenseFilter charterExpenseFilter;
	private CharterExpense selectedCharterExpsense;
	
	@PostConstruct
	public void init() {
		if(charterExpenseFilter == null) {
			charterExpenseFilter = new CharterExpenseFilter();
		}
	}
		
	
	public Map<String, String> getCharterExpensivesTypes() {
		if (charterExpensivesTypes == null){
			List<CharterExpenseType> charterT = getCharterExpenseTypeService().getAllCharterExpenseTypes();		
			
			charterExpensivesTypes = new HashMap<String, String>();
			
			for (CharterExpenseType charterType : charterT) {
				charterExpensivesTypes.put(FacesUtil.getMessage(charterType.getDescription()), String.valueOf(charterType.getId()));
			}
		}
		
		return charterExpensivesTypes;
	}
	
	protected CharterExpenseType obtainCharterType(String id) {
		List<CharterExpenseType> charterT = getCharterExpenseTypeService().getAllCharterExpenseTypes();	
		for (CharterExpenseType charterType : charterT) {
			if(id.equals(String.valueOf(charterType.getId()))) {
				return charterType;
			}
		}
		return null;
	}

	public ICharterExpenseTypeService getCharterExpenseTypeService() {
		return charterExpenseTypeService;
	}

	public void setCharterExpenseTypeService(
			ICharterExpenseTypeService charterExpenseTypeService) {
		this.charterExpenseTypeService = charterExpenseTypeService;
	}

	public void setCharterExpensivesTypes(Map<String, String> charterExpensivesTypes) {
		this.charterExpensivesTypes = charterExpensivesTypes;
	}
	
	public void search() {
		charterExpenseLazyDataModel = null;
	}
	
	public void reset() {
		charterExpenseLazyDataModel = null;
		charterExpenseFilter = new CharterExpenseFilter();
	}

	public CharterExpenseLazyDataModel getCharterExpenseLazyDataModel() {
		if (charterExpenseLazyDataModel == null) {
			charterExpenseLazyDataModel = new CharterExpenseLazyDataModel(getCharterExpenseService(), getCharterExpenseFilter());
		}
		return charterExpenseLazyDataModel;
	}

	public void setCharterExpenseLazyDataModel(
			CharterExpenseLazyDataModel charterExpenseLazyDataModel) {
		this.charterExpenseLazyDataModel = charterExpenseLazyDataModel;
	}

	public CharterExpenseFilter getCharterExpenseFilter() {
		return charterExpenseFilter;
	}

	public void setCharterExpenseFilter(CharterExpenseFilter charterExpenseFilter) {
		this.charterExpenseFilter = charterExpenseFilter;
	}

	public ICharterExpenseService getCharterExpenseService() {
		return charterExpenseService;
	}

	public void setCharterExpenseService(
			ICharterExpenseService charterExpenseService) {
		this.charterExpenseService = charterExpenseService;
	}
	
	public String viewCharterExpense(CharterExpense expense) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, expense.getCharter());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER_EXPENSE, expense);
		
		return BusinessSettings.VIEW_DETAILS;
	}

	public void showDialog(CharterExpense charter) {
		selectedCharterExpsense = charter;
	}
	
	public void delete() {
		getCharterExpenseLazyDataModel().setRowCount(getCharterExpenseLazyDataModel().getRowCount() -1 );
		getCharterExpenseService().deleteCharterExpense(getSelectedCharterExpsense());
		FacesUtil.showInfoMessage("searchCharterForm", "charterExpenseDeleted");
	}

	public CharterExpense getSelectedCharterExpsense() {
		return selectedCharterExpsense;
	}

	public void setSelectedCharterExpsense(CharterExpense selectedCharterExpsense) {
		this.selectedCharterExpsense = selectedCharterExpsense;
	}
	
	public void unselect() {
		selectedCharterExpsense = null;
	}
	
	
	public String editCharterExpense(CharterExpense expense) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, expense.getCharter());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER_EXPENSE, expense);
		
		return BusinessSettings.EDIT;
	}
	
}
